06. Running Python (Mac) — Overview

Running Python (Mac) — Overview

*You should now have Python 2.7.9 (or later) installed on your Mac. Here, we’ll give an overview of two ways to run Python code on your computer. In the pages that follow, you’ll find more detailed instructions for each method. We strongly recommend becoming familiar with both methods. *

METHOD #1: Using a Text Editor

Earlier in this Nanodegree program, you used a text editor to write code in HTML and CSS, and you saved that code as the appropriate file types by including extensions like .html and .css, respectively. In this stage of the Nanodegree, you’ll be using your text editor to write code in Python, so you’ll need to save files with the extension .py — for instance, test.py.

Many text editors provide a convenient feature that allows us to run code and receive its output right inside the text editor application. In some editors (like Sublime Text), this process can be as straightforward as saving a file with a .py extension and then asking the editor to run (or “build”) that code. In the page that follows, you'll find more detailed instructions for running Python code with two common text editors — Sublime Text and Atom. If you’re using a different editor and would like to implement this feature, you can search Google for instructions on how to do so, or you can head to the Udacity Discussion Forums to ask for guidance.

METHOD #2: Using the Command Line

Using a text editor to run and display the output of Python code may be convenient, but most developers also know how to run code using something called the command line. That process still starts with writing and saving code in a text editor, but instead of then asking the text editor application to run (or "build") that code, we run our Python file by typing a command into a command line interface (or CLI).

What is a Command Line Interface (CLI)?

A command line interface (CLI) is a text-based interface in which you can give the computer commands and receive some output. You can think of it as a direct conversation between a computer and its user.

The CLI that we’ll be using on Mac is accessed by running an application called Terminal. When you open the Terminal, you’ll enter a CLI referred to as the shell. There are different types of shells, but on Mac, the default is called bash. In this shell environment, the user types a text command, hits the RETURN key to run that command, and receives a response or output when the computer is done executing the instruction.

Why would we use a CLI to run code?

Using the command line to execute commands can be efficient because it doesn’t rely on any graphical user interface (or GUI) in which a user must click through windows or menus of pre-defined buttons and icons. Knowing how to use the shell is even necessary for some developers who may need to log in to remote servers on which a shell is available but a GUI is not. Moreover, writing shell commands (or the process of shell scripting) can offer immense flexibility and power to developers — for example, a one-line command can create, move, or delete large numbers of files or perform a variety of complex tasks. Lastly, being able to navigate your computer and execute commands through a CLI is simply a useful skill to have. Investing the time now to learn how to use the command line will pay off later.

How is a CLI accessed on Mac?

On Mac, you’ll use the Terminal application to access the command line. In the pages that follow, you’ll find detailed instructions for using the Terminal to run Python code.